<?php
class Blacklist {

	private $message = "";
	private $auth = null;
	function Blacklist() {
		$this->auth = new Authority();
	}
	
	public function run() {
		$p = '/@blacklist[\s]+((<-)|(->))([0-9]{4,15})[\s]*([0-9]*)/si';
		preg_match($p, trim($_POST[Message]), $n);
		if ("<-" == $n[1]) {
			$this->addBlacklist($n);
		} else
			if ("->" == $n[1]) {
				$this->delBlacklist($n);
			} else {
				$this->showBlacklist();
			}

	}
	private function addBlacklist($n) {
		$start_time = date("YmdHis", (time() + 8 * 3600));
		if (!$n[4]) {
			$this->message = "无效的qq号码~！";
			return;
		}
		$qq = $n[4];
		if($this->auth->adminLevel > 4) {
					$ret = @ DB :: GetQueryResult("select * from td_blacklist where qq='{$qq}' and clusternum='0'", true);
		} else {
					$ret = @ DB :: GetQueryResult("select * from td_blacklist where qq='{$qq}' and clusternum='{$_POST[ClusterNum]}'", true);
		}
		$day = 1;

		if ($n[5]) {
			$day = trim($n[5]);
			if ($day > 999 || $day < 1) {
				$this->message = "封禁天数不合理,正常为1~999";
				return;
			}
		}
		$end_time = date("YmdHis", (time() + 8 * 3600 + $day * 24 * 3600));
		if ($ret) {
			if($this->auth->adminLevel > 4) {
				@ DB :: Query("update  td_blacklist set start_time ='{$start_time}', end_time='{$end_time}',day={$day} ,flag=1 where qq='{$qq}' and clusternum='0'");
				$this->message = "修改成功, 在未来的{$day}天内,该qq号码({$qq})在所有群的一切发言都会被索妮娅忽略";
			} else {
				@ DB :: Query("update  td_blacklist set start_time ='{$start_time}', end_time='{$end_time}',day={$day} ,flag=1 where qq='{$qq}' and clusternum='{$_POST[ClusterNum]}'");
				$this->message = "修改成功, 在未来的{$day}天内,该qq号码({$qq})在本群的一切发言都会被索妮娅忽略";
			}
			
			return;
		} else {
			if($this->auth->adminLevel > 4) { 
				$clu = "0";
			}else{
				$clu = $_POST[ClusterNum];
			}
			$arr = array (
				'qq' => $qq,
				'clusternum' => $clu,
				'start_time' => $start_time,
				'end_time' => $end_time,
				'day' => $day
			);
			DB :: SaveTableRow('td_blacklist', $arr);
			if($this->auth->adminLevel > 4) {
				$this->message = "添加成功, 在未来的{$day}天内,该qq号码({$qq})在所有群的一切发言都会被索妮娅忽略";
			} else {
				$this->message = "添加成功, 在未来的{$day}天内,该qq号码({$qq})在本群的一切发言都会被索妮娅忽略";
			}
		}

	}
	private function delBlacklist($n) {
		$qq = $n[4];
		if($this->auth->adminLevel>4) {
				$sql1 = "select * from td_blacklist where qq='{$qq}' ";
				$sql2 = "update td_blacklist set flag=0 where qq='{$qq}' ";
			}else {
				$sql1 = "select * from td_blacklist where qq='{$qq}' and clusternum='{$_POST[ClusterNum]}'";
				$sql2 = "update td_blacklist set flag=0 where qq='{$qq}' and clusternum='{$_POST[ClusterNum]}' ";
			}
		$ret = @ DB :: GetQueryResult($sql1, true);
		if (!$n[4]) {
			$this->message = "无效的qq号码~！";
			return;
		}
		
		if ($ret) {
			
			@ DB :: Query($sql2);
			$this->message = "从黑名单移除成功>3<";

		} else {
			$this->message = "该用户还不在本群黑名单里面";
		}
		return;
	}
	private function showBlacklist() {
		$ret2 = @ DB :: GetQueryResult("select * from td_blacklist where flag=1 and  clusternum='{$_POST[ClusterNum]}'", false); //配置参数初始化
		$mess = "";
		$count = count($ret2);
		if ($count != 0) {
			$mess .= "本群黑名单里面共有{$count}人,分别为：\n";
			foreach ($ret2 as $key => $v) {
				$mess .= "qq号码：{$v[qq]} \t被封禁：{$v[day]}天\t解封日期：{$v[end_time]}\n";
			}
		} else {
			$mess .= "本群黑名单里目前还没有任何人\n";
		}
		$this->message = $mess . "\n" .
		"使用下列命令来添加和删除黑名单成员：\n" .
		"@blacklist <-qq号码 封禁天数    添加或者修改qq号码到黑名单   \n" .
		"@blacklist ->qq号码             把该qq从本群的黑名单移除    \n" .
		"例子：\n" .
		"@blacklist <-261589289 4   意思就是吧261589289这个用户放入黑名单4天\n" .
		"@blacklist ->261589289     意思就是吧261589289这个用户从黑名单中移除";
	}
	
	public function isInBlacklist() {
		myEcho($this->auth->adminLevel);	
		if ($this->auth->adminLevel > 4)
			return false;
			myEcho("ggg");	
		$start_time = date("YmdHis", (time() + 8 * 3600));
		$sql = "select qq from td_blacklist where flag='1' and qq={$_POST[Sender]} and clusternum in ('{$_POST[ClusterNum]}','0') and  end_time>'{$start_time}'" .
				" union all SELECT qq from td_normal_user_info where qq={$_POST[Sender]} and is_blacklist=1" ;

		//myEcho($sql);
		$ret2 = @ DB :: GetQueryResult($sql, true);
		if ($ret2)
			return true;
		else
			return false;

	}
	
	private function __get($property_name) {
		return $this-> $property_name;
	}

	private function __set($property_name, $value) {
		$this->$property_name = $value;
	}

}
?>